package com.example.Lezedata.ui.activity.setting;

import androidx.appcompat.app.AppCompatActivity;
import com.example.Lezedata.R;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.common.TransitionPage;
import com.example.Lezedata.ui.activity.login.ForgetPsdVc;
import com.example.Lezedata.util.GBToast;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import bid.Bid;

public class PinUpdateVc extends GBBaseVc {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_pin_update_vc;
    }

    @Override
    public CharSequence title() {
        return "PIN Setting";
    }

    private EditText CurrentPassword;
    private EditText NewPassword;
    private EditText Confirm;
    private TextView Submit;
    private TextView ForgottenPassword;
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        findView();
        addAction();
    }

    void findView(){
        CurrentPassword = findViewByTag("CurrentPassword");
        NewPassword = findViewByTag("NewPassword");
        Confirm = findViewByTag("Confirm");
        Submit = findViewByTag("Submit");
        ForgottenPassword = findViewByTag("ForgottenPassword");
    }

    void addAction(){
        Submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
        ForgottenPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpActivity(ForgetPINVc.class);
            }
        });
    }

    void submit(){
        if (CurrentPassword.getText().toString().isEmpty())
        {
            GBToast.shortToast("current Pin not null");
            return;
        }
        if (NewPassword.getText().toString().isEmpty())
        {
            GBToast.shortToast("new Pin not null");
            return;
        }
        if (Confirm.getText().toString().isEmpty())
        {
            GBToast.shortToast("confirm Pin not null");
            return;
        }
        if (!Confirm.getText().toString().equals(CurrentPassword.getText().toString()) )
        {
            GBToast.shortToast("confirm Pin and new Pin must same");
            return;
        }

        Bid.UpdatePwRequest request = Bid.UpdatePwRequest.newBuilder()
                .setNewPw(NewPassword.getText().toString())
                .setOldPw(CurrentPassword.getText().toString())
                .build();
        GBNetRequest.stubUser().updateTransPw(request, ServerResponseObserver.observer(new ServerResponseObserver.requestListener() {
            @Override
            public void complete(Object value, GBError error) {
                if (error == null){
                    submitFinish();
                }
            }
        }));
    }

    void submitFinish(){
        Intent intent = new Intent(mContext, TransitionPage.class);
        intent.putExtra(TransitionPage.Key_title,"Submit Successfully!");
        intent.putExtra(TransitionPage.Key_image,R.drawable.result_submit_success);
        jumpActivityIntent(intent);
    }
}